package com.afalon.cloud;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;

public class StringExtensions {

	public static boolean IsNullOrEmpty(String s) {
		// return s == null ? true : s.trim().length() == 0 ? true : false;
		return s == null || s.trim().length() == 0;
	}
	
	public static String GenerateNonce()
	{
		return Base64.encodeBytes(UUID.randomUUID().toString().getBytes()) ;
	}

	public static String GenerateMD5(String value) {
		try {
			MessageDigest m = MessageDigest.getInstance("MD5");

			m.update(value.getBytes(), 0, value.length());
			return leftPad(new BigInteger(1, m.digest()).toString(16),32,'0');
		} catch (NoSuchAlgorithmException e) {
		}
		return "ERROR";
	}
	
	public static Boolean ValidateMD5(String Value, String Data) {
		return Value.equals(GenerateMD5(Data));
	}

	public static String GenerateKeyedMD5(String value, String myKeyString) {
		try {
			PBEKeySpec keySpec = new PBEKeySpec(myKeyString.toCharArray());

			SecretKeyFactory skf = SecretKeyFactory
					.getInstance("PBEWithMD5AndDES");

			SecretKey key = skf.generateSecret(keySpec);

			// I use "hmacMD5" here because when I use
			// key.getAlgorithm() it returns nothing
			Mac mac = Mac.getInstance("hmacMD5");
			mac.init(key);

			// Encode the string into bytes using utf-8 and digest it
			byte[] utf8 = value.getBytes("utf8");
			byte[] digest = mac.doFinal(utf8);

			// convert the digest into a string
			// String digestB64 = new sun.misc.BASE64Encoder().encode(digest);
			return Base64.encodeBytes(digest);

		} catch (InvalidKeyException e) {
		} catch (NoSuchAlgorithmException e) {
		} catch (UnsupportedEncodingException e) {
		} catch (InvalidKeySpecException e) {
		}

		return "ERROR";

	}

	public static Boolean ValidateKeyedMD5(String Value, String Data, String Key) {
		return Value.equals(GenerateKeyedMD5(Data, Key));
	}

	public static Date StringToDate(String dateString) {
		Calendar c = Calendar.getInstance();
		c.set(Integer.parseInt(dateString.substring(0, 4)),
				Integer.parseInt(dateString.substring(4, 6)),
				Integer.parseInt(dateString.substring(6, 8)));

		return c.getTime();
	}

	public static String DateToString(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return String.format("%04d", c.get(Calendar.YEAR))
				+ String.format("%02d", c.get(Calendar.MONTH) + 1)
				+ String.format("%02d", c.get(Calendar.DAY_OF_MONTH));
	}
	
	public static String leftPad(String s, int width, char character) {
        return String.format("%" + width + "s", s).replace(' ', character);
    }

    public static String rightPad(String s, int width, char character) {
        return String.format("%-" + width + "s", s).replace(' ', character);
    }

}
